import dataBase
from mainLib import *
from spider import spider
import threading

def worker(id,db,hashdb,savepage,reproduce):
    try:
        spider(db=db, hashdb=hashdb,savepage = savepage,reproduce = reproduce)
    except Exception as e:
        print(e)
    finally:
        print(str(id) + ' complete')

def singleCpu(savepage,reproduce):
        
        dbName = 'data.json'
        hashDbName = 'hash.json'
        db = dataBase.Database(dbName)
        hashdb = dataBase.Database(hashDbName)
        if db.length() <=1 :
            return False
        threads = []
        for i in range(30):
            thread = threading.Thread(target=worker, args=(i,db,hashdb,savepage,reproduce), name=f"Thread-{i}")
            threads.append(thread)
            thread.start()
        for thread in threads:
            thread.join()

